// 利用 H5 的 localstorage 特性存储
export default {
  fetch (key) {
    let cacheData = JSON.parse(localStorage.getItem(key) || '{}')
    if (cacheData && typeof cacheData['tm'] !== 'undefined') {
      let timeStamp = cacheData['tm']
      let nowTime = new Date().getTime()
      if ((nowTime - timeStamp) >= (1000 * 60 * 60)) {
        console.log('缓存失效 key = ' + key)
        return undefined
      } else {
        console.log('缓存命中 key = ' + key + ' , date =  ', cacheData['data'])
        return cacheData['data']
      }
    }
    console.log('缓存不存在 key = ' + key)
    return undefined
  },
  save (key, items) {
    let cache = {
      tm: new Date().getTime(),
      data: items
    }
    console.log('开始缓存 key = ' + key + ', data = ', items)
    localStorage.setItem(key, JSON.stringify(cache || ''))
  }
}
